<?php

namespace App\Eloquent\Ygt;

use Framework\BaseClass\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Eloquent\Ygt\DbEloquent;
use DB;

class Stock extends DbEloquent
{
    use SoftDeletes;
    protected $table = 'ygt_stock';
    protected $dates = ['deleted_at'];
    protected $dateFormat = 'U';

    public function getStockList($where, $field, $limit='', $offset='')
    {
        $join = [
//            ['table'=>'ygt_stockin','field_l'=>'ygt_stockin.stock_id','field_c'=>'=','field_r'=>'ygt_stock.id'],
            ['table'=>'ygt_product','field_l'=>'ygt_product.id','field_c'=>'=','field_r'=>'ygt_stock.product_id'],
            ['table'=>'ygt_user','field_l'=>'ygt_user.id','field_c'=>'=','field_r'=>'ygt_stock.operate_uid']
        ];
        return self::getData( $where, $field, $limit, $offset, '', '', $join );
//        return self::getData($where, '*', $limit, $offset);
    }

    public function addStock($saveData)
    {
        $StockInfo = self::firstOrNew(['water_no' => $saveData['water_no']]);
        if ($StockInfo->id) xThrow(ERR_WATER_NUMBER_EXIST);

        if($this->saveStock($StockInfo, $saveData)){

            return $StockInfo->id;
        }else{
            return false;
        }
    }

    public function updateStock($StockId, $saveData)
    {
        $StockInfo = self::find($StockId);
        if (!$StockInfo) xThrow(ERR_CLIENT_USER_NOT_EXIST);

        return $this->saveStock($StockInfo, $saveData);
    }

    private function saveStock(self $StockInfo, array $saveData)
    {
        $StockInfo->fill($saveData);
        return $StockInfo->save();
    }

    //根据type不同联合的表不同
    //默认不联合表
    public static function getJoinList($where,$limit,$offset,$type=1,$orderby=['ygt_stock.id','desc'])
    {
        switch($type)
        {
            case 1:
                //取材料的流水列表
                $join = [
                    ['table'=>'ygt_product','field_l'=>'ygt_product.id','field_c'=>'=','field_r'=>'ygt_stock.product_id'],
                    ['table'=>'ygt_user','field_l'=>'ygt_user.id','field_c'=>'=','field_r'=>'ygt_stock.operate_uid'],
                ];
                $column             = 'ygt_stock.id,ygt_stock.water_no,ygt_stock.product_id';
                $column             .= ',ygt_stock.number,ygt_stock.type,ygt_stock.relate_type';
                $column             .= ',ygt_stock.last_product_number,ygt_stock.operate_uid';
                $column             .= ',ygt_stock.relate_id,ygt_stock.created_at,ygt_stock.place_name';
                $column             .= ',ygt_product.product_name,ygt_product.product_no';
                $column             .= ',ygt_user.truename as operate_name';
                break;
            default:
                $column             = 'ygt_stock.*';
                $join               = '';
        }
        $groupby            = '';
        $result             = self::getList($where,$column,$limit,$offset,$orderby,$groupby,$join);
        return $result;
    }
    //取客户的材料库存数量
    public static function getCustomerProductNumberList($customerId)
    {
        $query          = self::query();
        $selectSql      = 'product_id,SUM(number) as product_number';
        $query          = $query->selectRaw($selectSql);
        $query          = $query->where('customer_id','=',$customerId);
        $query          = $query->groupBy('product_id');
        $result         = $query->get()->toArray();
        return $result;
    }

    public static function getFormsData( $arr, $limit, $offset )
    {
        $company_id         = $arr['company_id'];
        $relate_type        = $arr['relate_type'];
        $start_time         = $arr['start_time'];
        $end_time           = $arr['end_time'];
        //取列表
        $sum                = DB::raw('SUM(a.total_price) AS total_price_sum');
        $query              = DB::table('ygt_stock as a');
        $query              = $query->select( 'a.product_id',$sum );
        $query              = $query->where('a.company_id', '=', $company_id);
        $query              = $query->where('a.relate_type', '=', $relate_type);
        $query              = $query->where('a.created_at', '>', $start_time);
        $query              = $query->where('a.created_at', '<', $end_time);
        $query              = $query->groupBy('a.product_id');
        $query              = $query->orderBy('total_price_sum','desc');
        $query              = $query->limit($limit);
        $query              = $query->offset($offset);
        $lists              = $query->get();
        //取全部总价
        $sum                = DB::raw('SUM(a.total_price) AS total_price_sum');
        $query              = DB::table('ygt_stock as a');
        $query              = $query->select( 'a.product_id',$sum );
        $query              = $query->where('a.company_id', '=', $company_id);
        $query              = $query->where('a.relate_type', '=', $relate_type);
        $query              = $query->where('a.created_at', '>', $start_time);
        $query              = $query->where('a.created_at', '<', $end_time);
        $query              = $query->groupBy('a.product_id');
        $query              = $query->orderBy('total_price_sum','desc');
        $all_lists          = $query->get()->toArray();
        $total_price_sum_all=0;
        foreach( $all_lists as $key=>$val)
        {
            $total_price_sum_all+=$val->total_price_sum;
        }
        $total_price_sum_all=floatval($total_price_sum_all);
        //取总价和最高的一条
        $sum                = DB::raw('SUM(a.total_price) AS total_price_sum');
        $query              = DB::table('ygt_stock as a');
        $query              = $query->select( 'a.product_id',$sum );
        $query              = $query->where('a.company_id', '=', $company_id);
        $query              = $query->where('a.relate_type', '=', $relate_type);
        $query              = $query->where('a.created_at', '>', $start_time);
        $query              = $query->where('a.created_at', '<', $end_time);
        $query              = $query->groupBy('a.product_id');
        $query              = $query->orderBy('total_price_sum','desc');
        $max_one            = $query->first();
        //取总条数
        $sum                = DB::raw('SUM(a.total_price) AS total_price_sum');
        $query              = DB::table('ygt_stock as a');
        $query              = $query->select( 'a.product_id',$sum );
        $query              = $query->where('a.company_id', '=', $company_id);
        $query              = $query->where('a.relate_type', '=', $relate_type);
        $query              = $query->where('a.created_at', '>', $start_time);
        $query              = $query->where('a.created_at', '<', $end_time);
        $query              = $query->groupBy('a.product_id');
        $count              = $query->get()->count();

        $result             = [ 'lists'=>$lists, 'count'=>$count, 'max_one'=>$max_one,'total_price_sum_all'=>$total_price_sum_all ];
        return $result;
    }
}
